package com.appbee.cm.client.regionunit.ui;

import java.util.List;

import javax.inject.Inject;

import com.appbee.cm.client.modular.presenter.ModulSelectionEvent;
import com.appbee.cm.client.modular.ui.ModulSelectionBox;
import com.appbee.cm.client.regionunit.presenter.RegionActivity;
import com.appbee.cm.client.regionunit.presenter.StyleInfo;
import com.appbee.core.client.editors.LabelIntegerbox;
import com.appbee.core.client.modular.IModul;
import com.appbee.core.client.modular.SettingsView;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.text.shared.AbstractRenderer;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.UIObject;
import com.google.gwt.user.client.ui.ValueListBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ProvidesKey;

public class ModulEditView extends Composite implements
		RegionActivity.EditView
{

	private static ModulEditViewUiBinder uiBinder = GWT
			.create(ModulEditViewUiBinder.class);

	interface ModulEditViewUiBinder extends UiBinder<Widget, ModulEditView>
	{
	}

	@UiField(provided=true)
	ModulSelectionBox modul;

	@UiField(provided = true)
	ValueListBox<StyleInfo> styleList = new ValueListBox<StyleInfo>(
			new StyleInfoRender(), new StyleInfoKeyProvider());

	@UiField
	LabelIntegerbox number;

	@UiField
	SimplePanel settingsPanel;

	@UiField
	Button saveButton;

	private final PopupPanel popup = new PopupPanel(true, true);

	private RegionActivity.ModulPresenter presenter;

	@Inject
	public ModulEditView(EventBus eventBus, ModulSelectionBox modulBox)
	{
		modul = modulBox;
		initWidget(uiBinder.createAndBindUi(this));
		popup.setWidget(this);
		saveButton.addClickHandler(new ClickHandler()
		{

			@Override
			public void onClick(ClickEvent event)
			{
				presenter.doSave();
			}
		});
		ModulSelectionEvent.register(	eventBus,
										modul,
										new ModulSelectionEvent.Handler()
		{

			@Override
											public void onChange(ModulSelectionEvent event)
			{
												presenter.doModulSelected(event
														.getModul());
			}
		});
	}

	@Override
	public void show(Object source)
	{
		popup.showRelativeTo((UIObject) source);
	}

	@Override
	public void hide()
	{
		popup.hide();
	}

	@Override
	public IModul getSelectedModul()
	{
		return modul.getValue();
	}

	@Override
	public void setStyles(List<StyleInfo> styles)
	{
		styleList.setAcceptableValues(styles);
	}

	@Override
	public void setSelectedStyle(StyleInfo style)
	{
		styleList.setValue(style);
	}

	@Override
	public StyleInfo getSelectedStyle()
	{
		return styleList.getValue();
	}

	@Override
	public void setSelectedModul(IModul selectedModul)
	{
		modul.getPresenter().setValue(selectedModul);
	}

	@Override
	public int getNumber()
	{
		Integer value = number.getValue();
		return value == null ? 0 : value;
	}

	@Override
	public void setNumber(int nr)
	{
		number.setValue(nr);
	}

	@Override
	public void setSettingsView(SettingsView settingsView)
	{
		settingsPanel.setWidget(settingsView);
	}

	@Override
	public void setPresenter(RegionActivity.ModulPresenter presenter)
	{
		this.presenter = presenter;
	}

	public class ModulRender extends AbstractRenderer<IModul>
	{

		@Override
		public String render(IModul object)
		{
			return object == null ? "" : object.getName();
		}
		
	}
	
	public class ModulKeyProvider implements ProvidesKey<IModul>
	{

		@Override
		public Object getKey(IModul item)
		{
			return item == null ? null : item.getId();
		}
		
	}

	public class StyleInfoRender extends AbstractRenderer<StyleInfo>
	{

		@Override
		public String render(StyleInfo object)
		{
			return object == null ? "" : object.getName();
		}

	}

	public class StyleInfoKeyProvider implements ProvidesKey<StyleInfo>
	{

		@Override
		public Object getKey(StyleInfo item)
		{
			return item == null ? null : item.getId();
		}

	}
}
